import 'dart:async';

import 'package:flare_flutter/flare_actor.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sui_shou_pai/Utils/aes_util.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/login/login.dart';
import 'package:sui_shou_pai/module/color.dart';
import 'package:sui_shou_pai/module/effect.dart';
import 'package:sui_shou_pai/network/http_request.dart';

class NewPasswordPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return NewPasswordState();
  }
}

class NewPasswordState extends State<NewPasswordPage> {
  String animationType = "idle";
  final passFocusNode = FocusNode();
  bool passwordVisible = true;
  TextEditingController _passwordController = new TextEditingController();

  @override
  void initState() {
    super.initState();
    passFocusNode.addListener(() {
      if (passFocusNode.hasFocus) {
        setState(() {
          animationType = "hands_up";
        });
      } else {
        setState(() {
          animationType = "hands_down";
        });
        Future.delayed(Duration(milliseconds: 500), () {
          setState(() {
            animationType = "idle";
          });
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        child: Scaffold(
          body: Stack(
            children: <Widget>[
              Container(
                height: 300,
                color: greyF3F3F3,
                padding: EdgeInsets.only(top: 60, bottom: 30),
                child: FlareActor('assets/Teddy.flr',
                    shouldClip: false,
                    alignment: Alignment.bottomCenter,
                    fit: BoxFit.contain,
                    animation: animationType),
              ),
              Container(
                width: MediaQuery.of(context).size.width,
                margin: EdgeInsets.only(top: 270),
                decoration: BoxDecoration(
                  borderRadius: borderRadiusOnlyLeftTop24(),
                  color: Colors.white,
                ),
                child: ListView(
                  padding: EdgeInsets.symmetric(vertical: 36, horizontal: 44),
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.only(bottom: 24),
                      child: Text(
                        '输入新密码',
                        style: TextStyle(
                            fontSize: 40, fontWeight: FontWeight.bold),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.symmetric(vertical: 10),
                      child: Container(
                        height: 54,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          borderRadius: borderRadiusAll8(),
                          color: greyF6F6F6,
                        ),
                        child: TextFormField(
                          obscureText: passwordVisible,
                          controller: _passwordController,
                          maxLines: 1,
                          focusNode: passFocusNode,
                          decoration: InputDecoration(
                            border: InputBorder.none,
                            hintText: '密码',
                            prefixIcon: Icon(Icons.lock_outline),
                            hintStyle:
                                TextStyle(fontSize: 18, color: Colors.grey),
                            suffixIcon: IconButton(
                              icon: Icon(
                                //根据passwordVisible状态显示不同的图标
                                passwordVisible
                                    ? Icons.visibility
                                    : Icons.visibility_off,
                                color: Theme.of(context).primaryColorDark,
                              ),
                              onPressed: () {
                                //更新状态控制密码显示或隐藏
                                setState(() {
                                  passwordVisible = !passwordVisible;
                                });
                              },
                            ),
                          ),
                          style: TextStyle(fontSize: 18),
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 30),
                      child: MaterialButton(
                        onPressed: () {
                          _submit();
                        },
                        child: Text(
                          '确认',
                          style: TextStyle(
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                              color: Colors.white,
                              letterSpacing: 2),
                        ),
                        color: Colors.blueAccent,
                        elevation: 0,
                        minWidth: 400,
                        height: 54,
                        textColor: Colors.white,
                        shape: RoundedRectangleBorder(
                            borderRadius: borderRadiusAll8()),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        onTap: () {
          hindKeyBoarder();
        });
  }

  void hindKeyBoarder() {
    //输入框失去焦点
    passFocusNode.unfocus();
    //隐藏键盘
    SystemChannels.textInput.invokeMethod('TextInput.hide');
  }

  void _submit() async {
    String phone = await StorageUtil.getStringItem("phone_password");
    String password = _passwordController.text;
    HttpRequest.request("/employee/updatePasswordbyPhone",
            method: "post",
            data: {"phone": phone, "newPassword": AesUtil.aesEncrypt(password)})
        .then((value) {
      print(value);
      if (value.data["code"] == 200) {
        Fluttertoast.showToast(msg: "修改成功");
        Navigator.push(
            context, new MaterialPageRoute(builder: (context) => LoginPage()));
      } else {
        Fluttertoast.showToast(msg: "无此账号");
      }
    });
  }
}
